package com.sunday.aomi.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.aomi.R;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/6/12.
 */

public class ForgetPwdActivity extends BaseActivity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.edit_phone)
    ClearEditText editPhone;
    @Bind(R.id.edit_code)
    ClearEditText editCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.edit_confirm_pwd)
    ClearEditText editConfirmPwd;
    @Bind(R.id.btn_confirm)
    TextView btnConfirm;

    TimeCount timeCount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        title.setText("忘记密码");
        timeCount=new TimeCount(60000,1000,sendCode);
    }




    private String mobile,code,pwd,confimrPwd;
    @OnClick({R.id.btn_confirm,R.id.send_code})
    void onClick(View v){
        switch (v.getId()){
            case R.id.btn_confirm:
                mobile=editPhone.getText().toString().trim();
                code=editCode.getText().toString().trim();
                pwd=editPwd.getText().toString().trim();
                confimrPwd=editConfirmPwd.getText().toString().trim();
                if (!StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext,"请输入正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)){
                    ToastUtils.showToast(mContext,"请输入正确验证码");
                    return;
                }
                if (TextUtils.isEmpty(pwd)||TextUtils.isEmpty(confimrPwd)){
                    ToastUtils.showToast(mContext,"请输入密码");
                    return;
                }
                if (!pwd.equals(confimrPwd)){
                    ToastUtils.showToast(mContext,"两次密码输入不一致");
                    return;
                }

                setPwd();

                break;
            case R.id.send_code:
                 mobile=editPhone.getText().toString().trim();
                if (!StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext,"请输入正确手机号");
                    return;
                }
                sendCode();
                break;
        }
    }

    private void sendCode(){
        String nonce= MD5Utils.random();
        String type="4";//忘记密码
        String[] str1=new String[]{"mobile","type","nonce"};
        String[] str2=new String[]{mobile,type,nonce};
        timeCount.start();
        Call<ResultDO> call= MemberClient.getApiAdapter().sendCode(mobile,type,nonce, SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"验证码已发送到手机");
                }else{
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    private void setPwd(){
        Call<ResultDO> call= MemberClient.getApiAdapter().forgetPassword(mobile,pwd,code);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"密码设置成功");
                    finish();
                }else{
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount!=null){
            timeCount.cancel();
        }
    }
}
